// pages/myself/thappointment.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        listInfo: [],
        showModalStatus: false,
        showTopTips: false,
        errorMsg: '',
        orderlists: [],
        planlists: [],
        loading: true,
        userinfo: '',
        uid:0,
        oid:0, //orderid
        cf:'',
        jjreason:'',
        isshow:false,
        showModalStatus: false,
        animationData: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      if (!app.globalData.isLoginAuth) {
        this.showModal();
      }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (e) {
      if (app.globalData.isLoginAuth) {
        this.setData({
          uid: wx.getStorageSync('userId'),
          listInfo: app.globalData.listInfo
        })
        this.getAppointlist();
      }
    },
    getAppointlist: function () {
        var that = this;
        var identity = wx.getStorageSync('roleFlag');
        wx.request({
            url: app.globalData.hostUrl + 'member/appointmentForsvrer',
            data: {
                identity: identity,
                uid: wx.getStorageSync('userId')
            },
            methods: 'GET',
            header: {
                "Content-Type": "json"
            },
            success: function (res) {
              console.log(res);
                if (res.data.result) {
                    that.setData({
                      orderlists: res.data.result.lists,
                      planlists: res.data.result.plxc
                    });
                }
                else {
                    that.setData({
                        loading: false
                    })
                    return false
                }
            }
        })
    },
   
    calling: function (e) {
        var mobile = e.currentTarget.dataset.phone;
        wx.makePhoneCall({
            phoneNumber: mobile,
            success: function () {
                //console.log("拨打电话成功！")
            },
            fail: function () {
                // console.log("拨打电话失败！")
            }
        })
    },
    getreason: function (e) {
      this.setData({
        jjreason: e.detail.value
      });
    },
    onRefuse: function () {
      var that = this;
      var id = that.data.oid;
      var cf = that.data.cf;
      var reason=that.data.jjreason;
      var finish =-1;
      if(reason==''){
        wx.showToast({
          title: '请填写拒绝理由',
          icon: 'error',
          duration: 2000
        })
        return false;
      }
      wx.showModal({
        content: '您确定拒绝本订单吗?',
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            wx.request({
              url: app.globalData.hostUrl + 'member/modifyfinish',
              data: {
                flag: cf,
                fval: finish,
                id: id,
                reason: reason
              },
              methods: 'GET',
              success: function (res) {
                if (res.data.code == 200) {
                  that.setData({
                    isshow: true
                  })
                  wx.navigateTo({
                    url: '../myself/thappointment',
                  })
                }
                else {
                  wx.showToast({
                    title: '操作失败',
                    icon: 'error',
                    duration: 2000
                  })
                }
              }
            })
          }
        }
      });

    },
    modifyfinish:function(e){
        var that=this;
        var id = e.currentTarget.dataset.oid;
        var cf = e.currentTarget.dataset.cf;
        var finish = e.currentTarget.dataset.finish;
        wx.showModal({
            content: '您确定此次操作吗?',
            showCancel: true,
            success: function (res) {
                if (res.confirm) {
                    wx.request({
                      url: app.globalData.hostUrl + 'member/modifyfinish',
                        data: {
                          flag: cf,
                          fval: finish,
                          id: id
                        },
                        methods: 'GET',
                        success: function (res) {
                            if(res.data.code==200){
                                that.setData({
                                    isshow:true
                                })
                                wx.navigateTo({
                                    url: '../myself/thappointment',
                                })
                            }   
                            else{
                                wx.showToast({
                                    title: '操作失败',
                                    icon: 'error',
                                    duration: 2000
                                })
                            } 
                        }
                    })
                }
            }
        });
      },
  goorderdetail: function (e) {
    var rid = e.currentTarget.dataset.id;
    var cf = e.currentTarget.dataset.cf;
    if (cf == 'xc') {
      var gotourl = '../xuecar/orderdetail?oid=' + rid;
    }
    else if (cf == 'pl') {
      var gotourl = '../traincar/orderdetail?oid=' + rid;
    }
    else if (cf == 'kp') {
      var gotourl = '../keepcar/orderdetail?oid=' + rid;
    }
    else if (cf == 'buy') {
      var gotourl = '../autocar/orderdetail?oid=' + rid;
    }
    wx.navigateTo({
      url: gotourl
    })
  },
  // 弹框
  openEdit: function (e) {
    console.log(e);
    this.setData({
      showModalStatus: true,
      oid: e.currentTarget.dataset.oid,
      cf: e.currentTarget.dataset.cf
    });
    util.clearError(this);
  },
  //關閉
  closeEdit: function (e) {
    this.setData({
      showModalStatus: false
    });
    util.clearError(this);
  },
  gohome: function () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  yuy: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/thappointment',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myappointment',
      })
    }
  },
  orders: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/trorder',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myorder',
      })
    }
  },
  mys: function () {
    wx.redirectTo({
      url: '../myself/myself',
    })
  },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      showModalStatus: false,
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e);
    if (e.detail.errMsg == 'getUserInfo:ok') {
      app.globalData.userInfo = e.detail.userInfo;
      app.getfansinfo();
    }
    else {
      wx.redirectTo({
        url: '../index/index',
      })
    }
  }
})